package hbr;

import java.awt.Color;
import robocode.HitByBulletEvent;
import robocode.HitWallEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;

/**
 * Implement the KillBot robot, which scans for enemy robot and attacks.
 * 
 * @author Bryson Y. Hori
 */
public class KillBot extends Robot {
  private boolean checkMove = false;
  private double enemyEnergy = 100;
  
  /**
   * Rotate radar to locate enemy robots.
   */
  public void run() {
    setBulletColor(Color.green);
    setBodyColor(Color.darkGray);
    setGunColor(Color.lightGray);
    setRadarColor(Color.black);
    
    while (true) {
      turnRadarRight(360);
    }
  }
  
  /**
   * Rotates gun towards enemy fire and does a few checks based on enemies position.
   * 
   * @param event contains information about the enemy robot, e.g. its location
   */
  @Override
  public void onScannedRobot(ScannedRobotEvent event) {
    double degree = event.getBearing();
    double heading = getHeading();
    double gunHeading = getGunHeading();
    double distance = event.getDistance();
    
    //Traces the enemy with gun
    turnGunRight(heading - gunHeading + degree);
    //Fires at enemy based on distance
    pullTrigger(distance);    
    checkEnemy(event);
  }
  
  /**
   * Changes direction of robot when hit wall.
   * 
   * @param event contains information about hitting wall
   */
  @Override
  public void onHitWall(HitWallEvent event) {
    turnRight(event.getBearing() + 90);
    ahead(50);
  }
  
  /**
   * Changes direction based on enemies bearing and moves away from fire.
   * 
   * @param event contains information about enemy
   */
  @Override
  public void onHitByBullet(HitByBulletEvent event) {
    if (!checkMove) {
      if (event.getBearing() + 90 > 180) {
        turnRight(event.getBearing() - 90);
      }
      else {
        turnRight(event.getBearing() + 90);
      }
      ahead(50);
    }
  }
  
  /**
   * Checks to see if robot should move or not based on enemy.
   * 
   * @param event ScannedRobotEvent used to get information of enemy
   * @return value error check for debugging
   */
  private boolean checkEnemy(ScannedRobotEvent event) {
    boolean value = false;
    if (event.getDistance() < 250) {
      checkMove = true;
      value = true;
    }
    else {
      checkMove = false;
      value = true;
      if (enemyEnergy - event.getEnergy() > 0 && enemyEnergy - event.getEnergy() <= 3) {
        if (event.getBearing() + 90 > 180) {
          turnRight(event.getBearing() - 90);
        }
        else {
          turnRight(event.getBearing() + 90);
        }
        ahead(50);
        enemyEnergy = event.getEnergy();
      }
    }
    return value;
  }
  
  /**
   * Helper method to shoot at enemy.
   * 
   * @param distance distance to the enemy robot
   * @return value boolean value to do debugging and check
   */
  private boolean pullTrigger(double distance) {
    boolean value = false;
    if (getGunHeat() == 0) {
      fire(Math.min(400 / distance, 3));
      value = true;
    }
    return value;
  }
}